//
//  VisitorView.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/16.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit
import SnapKit
/// 访客视图协议
protocol VisitorViewDelegate: NSObjectProtocol {
    
    
    func visitorViewDidRegister()
    
    func visitorViewDidLogin()
    
}


class VisitorView: UIView {
    /// 定义代理属性
    weak var delegate:VisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        
       super.init(frame: frame)
        
        //backgroundColor = UIColor.blueColor()
        
          setupUI()
        
        loginBtn.addTarget(self, action: "clickLoginBtn", forControlEvents: .TouchUpInside)
        
        reginsBtn.addTarget(self, action: "clickReginBtn", forControlEvents: .TouchUpInside)
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func clickLoginBtn(){
        
        delegate?.visitorViewDidLogin()
        
    }
    
    @objc func clickReginBtn(){
        
        delegate?.visitorViewDidRegister()
        
    }
 
    /// 设置访客视图信息
    ///
    /// - parameter imgName:   图像名 首页默认为nil
    /// - parameter titletext: 标签文字
    func setup(imgName: String? = nil,titletext: String){
        
        messageLabel.text = titletext
        
        guard let imgname = imgName else{
            //首页开启动画
            animationbegin()
            
            return
        }
        
        homeImgView.hidden = true
        
        visitordiscoverImg.hidden = true
        
        iconImgView.image = UIImage(named: imgname)
        
        
    }
    
/// 开启动画
    private func animationbegin(){
        
        let anmi = CABasicAnimation(keyPath: "transform.rotation")
        
        anmi.toValue = 2 * M_PI
        anmi.repeatCount = MAXFLOAT
        anmi.duration = 10
        //提示 对于需要循环播放的动画 可以设置此属性 为false
        //视图离开屏幕 后 不会被销毁  当动画所在视图被销毁后 动会会连带销毁
        
        anmi.removedOnCompletion = false
        iconImgView.layer.addAnimation(anmi, forKey: "anmi")
        
    }
  
    // MARK: - 私有属性
    /// 动画图标
    private lazy var iconImgView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    /// 小房子图标
    private lazy var homeImgView = UIImageView(imageName: "visitordiscover_feed_image_house")
    /// 标签文字
    private lazy var messageLabel: UILabel = UILabel(
        text: "关注一些人,可以看到更多",
        alignment: .Center)
    /// 登录按钮
    private lazy var loginBtn: UIButton = UIButton(gxl_title: "登录",    backImgName: "common_button_white_disable")
    /// 注册按钮
    private lazy var reginsBtn: UIButton = UIButton(
        gxl_title: "注册",
        titleColor: UIColor.orangeColor(),
        backImgName: "common_button_white_disable")
    /// 遮罩图片
    private lazy var visitordiscoverImg = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
  
}
// MARK: - 访客视图函数  //放在extension中的方法 1遵守协议的方法 2本类新建的方法，不是父类提供的
extension VisitorView {
     /// 设置UI界面
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        /// 添加子控件
        addSubview(iconImgView)
        addSubview(visitordiscoverImg)
        addSubview(homeImgView)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(reginsBtn)
        //图标视图
        iconImgView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            
            make.centerY.equalTo(self).offset(-60)
            
        }
        //小房子视图
        homeImgView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(iconImgView)
        }
        
        //标签视图
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImgView)
            make.top.equalTo(iconImgView.snp_bottom).offset(20)
            
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        
        //注册按钮
        reginsBtn.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLabel)
            
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        //登录按钮
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(reginsBtn)
            make.size.equalTo(reginsBtn)
        }
        
        //遮罩视图
        visitordiscoverImg.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(iconImgView)
        }
        
        
    
        
        
        
    }
    
   
    
}
